package com.itxiao.util;

import cn.hutool.json.JSONUtil;
import com.itxiao.pojo.bo.wx.Subscribe;
import io.swagger.util.Json;
import lombok.Data;


import javax.annotation.Resource;
import java.io.IOException;
import java.net.URI;
import java.net.URLEncoder;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class WxUtil {

    private static final String TOKEN_URL_FORMAT = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
    private static final String WX_LOGIN = "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code";
    private static final String WX_GET_PHONE = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=%s";

    //校验文字，图片和音频是否合法
    private static final String CHECK_TEXT_LEGAL = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=%s";
    private static final String CHECK_MEDIA_LEGAL = "https://api.weixin.qq.com/wxa/media_check_async?access_token=%s";

    //订阅消息
    private static final String SEND_SUBSCRIBE = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=%s";

    public static String getAccessToken(String appId, String secret) {
        String s = HttpUtil.get(String.format(TOKEN_URL_FORMAT, appId, secret), null);
        Map<String, Object> responseMap = JSONUtil.toBean(s, Map.class, false);
        Object data = responseMap.get("access_token");
        return (String) data;
    }
    //微信登录
    public static String Login(String appId, String secret, String code) {
        String s = HttpUtil.get(String.format(WX_LOGIN, appId, secret, code), null);
        Map<String, Object> responseMap = JSONUtil.toBean(s, Map.class, false);
        Object data = responseMap.get("open_id");
        return (String) data;
    }
   //得到用户手机号
    public static String GetPhone(String accessToken, String code) {
        Map<String, Object> params = new HashMap<>();
        params.put("code", code);
        String s = HttpUtil.postJson(String.format(WX_GET_PHONE, accessToken), JSONUtil.toJsonStr(params));
        Map<String, Object> responseMap = JSONUtil.toBean(s, Map.class, false);
        Object data = responseMap.get("phone_info");
        return (String) data;
    }
    //wx校验文本
    public static Boolean CheckTextLegal(String accessToken, String openId, String content, String scene) {
        Map<String, Object> params = new HashMap<>();
        params.put("content", content);
        params.put("openid", openId);
        params.put("scene", scene);
        params.put("version", 2);
        String s = HttpUtil.postJson(String.format(CHECK_TEXT_LEGAL, accessToken), JSONUtil.toJsonStr(params));
        Map<String, Object> responseMap = JSONUtil.toBean(s, Map.class, false);
        Object data = responseMap.get("errcode");
        return (Integer) data == 0;
    }
    //wx校验图片视频
    public static Boolean CheckMediaLegal(String accessToken, String mediaType, String openId, String mediaUrl, String scene) {
        Map<String, Object> params = new HashMap<>();
        params.put("media_url", mediaUrl);
        params.put("media_type", mediaType);
        params.put("openid", openId);
        params.put("scene", scene);
        params.put("version", 2);
        String s = HttpUtil.postJson(String.format(CHECK_MEDIA_LEGAL, accessToken), JSONUtil.toJsonStr(params));
        Map<String, Object> responseMap = JSONUtil.toBean(s, Map.class, false);
        Object data = responseMap.get("errcode");
        return (Integer) data == 0;
    }

    //订阅消息
    public static String sendSubscribe(String accessToken, Subscribe subscribe) {
        // 2. 构建请求 URL
        String url = String.format(SEND_SUBSCRIBE, accessToken);
        // 3. 发送 POST 请求
        String jsonBody = JSONUtil.toJsonStr(subscribe);
        String respBody = HttpUtil.postJson(url, jsonBody);
        Map<String, Object> responseMap = JSONUtil.toBean(respBody, Map.class, false);
        Object code = responseMap.get("errcode");
        Object msg = responseMap.get("errmsg");
        return (String) msg;
    }
}
